package thread.threadexecutor;

import org.junit.Test;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;

public class ExecutorsDemo {

  public void test1() {
    /**
     * 1 一个Timer对象中的执行器，共享一个工作线程(TimerThread)。所以统一时间只能有一个线程运行
     * 2 当 执行器被触发调用时，会计算任务的下一个nextExecutionTime(任务开始执行时间)
     * 3 period 这参数是指周期，周期的起始时间是任务的开始时间
     */
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " :线程1 " + new Date());
      }
    }, 1000, 10000);

    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        try {
          Thread.sleep(20000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " :线程2 " + new Date());
      }
    }, 1000, 1000);

  }

  public void test2() {
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
    scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " : " + new Date());
      }
    }, 1000, 1000, TimeUnit.MILLISECONDS);

    scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " : " + new Date());
      }
    }, 1000, 1000, TimeUnit.MILLISECONDS);

    while (true) {
      try {
        Thread.sleep(10000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
//    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2,
//            0L, TimeUnit.MILLISECONDS,
//            new LinkedBlockingQueue<Runnable>());
    System.out.println("start ...");
    new Thread(new Runnable() {
      @Override
      public void run() {
        while (true) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          System.out.println(Thread.currentThread().getName() + " ... ");
        }
      }
    }, "thread-1").start();

    new Thread(new Runnable() {
      @Override
      public void run() {
        while (true) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          System.out.println(Thread.currentThread().getName() + " ... ");
        }
      }
    }, "thread-1").start();

  }

}
